<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>泡泡玛特 - 登录/注册</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #8a2be2;
            --secondary: #ff6ec7;
            --accent: #00c8ff;
            --light: #f9f5ff;
            --dark: #2a0a4a;
            --background: #f8f7fc;
        }

        body {
            background-color: var(--background);
            color: #333;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            background-image: url('https://images.unsplash.com/photo-1606310737840-eba660f37a26?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80');
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            position: relative;
        }

        body::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(138, 43, 226, 0.3);
            z-index: 1;
        }

        .container {
            position: relative;
            z-index: 2;
            max-width: 1200px;
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        /* 顶部导航 */
        .header {
            width: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 0;
            margin-bottom: 40px;
        }

        .logo {
            display: flex;
            align-items: center;
            color: white;
            text-decoration: none;
            font-size: 28px;
            font-weight: 800;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }

        .logo i {
            margin-right: 10px;
            font-size: 32px;
        }

        .nav-icons {
            display: flex;
            gap: 20px;
        }

        .nav-icon {
            color: white;
            font-size: 22px;
            position: relative;
            cursor: pointer;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }

        .cart-count {
            position: absolute;
            top: -8px;
            right: -8px;
            background: var(--accent);
            color: white;
            font-size: 12px;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }

        /* 登录区域 */
        .login-container {
            display: flex;
            width: 100%;
            max-width: 1000px;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 20px 50px rgba(138, 43, 226, 0.4);
        }

        .login-image {
            flex: 1;
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 40px;
            position: relative;
            overflow: hidden;
        }

        .login-image::before {
            content: '';
            position: absolute;
            top: -50px;
            right: -50px;
            width: 200px;
            height: 200px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.1);
        }

        .login-image::after {
            content: '';
            position: absolute;
            bottom: -80px;
            left: -80px;
            width: 250px;
            height: 250px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.1);
        }

        .image-content {
            text-align: center;
            color: white;
            z-index: 2;
            max-width: 400px;
        }

        .image-content h2 {
            font-size: 36px;
            margin-bottom: 20px;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }

        .image-content p {
            font-size: 18px;
            line-height: 1.6;
            margin-bottom: 30px;
        }

        .toy-icon {
            font-size: 100px;
            margin-bottom: 30px;
            animation: float 3s ease-in-out infinite;
        }

        .login-form {
            flex: 1;
            padding: 60px 50px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            position: relative;
        }

        .login-form h2 {
            font-size: 32px;
            color: var(--dark);
            margin-bottom: 10px;
            text-align: center;
        }

        .login-form p {
            color: #777;
            text-align: center;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 25px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: var(--dark);
        }

        .input-with-icon {
            position: relative;
        }

        .input-with-icon i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--primary);
        }

        .form-control {
            width: 100%;
            padding: 14px 20px 14px 45px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s;
        }

        .form-control:focus {
            border-color: var(--primary);
            outline: none;
            box-shadow: 0 0 0 3px rgba(138, 43, 226, 0.2);
        }

        .btn {
            display: inline-block;
            padding: 14px 32px;
            background: var(--primary);
            color: white;
            text-decoration: none;
            font-weight: 600;
            border-radius: 10px;
            transition: all 0.3s;
            border: none;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            text-align: center;
        }

        .btn:hover {
            background: #7a1ad2;
            transform: translateY(-3px);
            box-shadow: 0 10px 20px rgba(138, 43, 226, 0.3);
        }

        .btn-accent {
            background: var(--accent);
            box-shadow: 0 4px 15px rgba(0, 200, 255, 0.4);
        }

        .btn-accent:hover {
            background: #00b3e6;
            box-shadow: 0 6px 20px rgba(0, 200, 255, 0.6);
        }

        .divider {
            display: flex;
            align-items: center;
            margin: 25px 0;
        }

        .divider::before,
        .divider::after {
            content: '';
            flex: 1;
            height: 1px;
            background: #e0e0e0;
        }

        .divider span {
            padding: 0 15px;
            color: #777;
        }

        .social-login {
            display: flex;
            gap: 15px;
            margin-bottom: 30px;
        }

        .social-btn {
            flex: 1;
            padding: 12px;
            border-radius: 10px;
            border: 1px solid #e0e0e0;
            background: white;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s;
        }

        .social-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .social-btn i {
            font-size: 20px;
            margin-right: 8px;
        }

        .wechat i {
            color: #09bb07;
        }

        .qq i {
            color: #12b7f5;
        }

        .weibo i {
            color: #e6162d;
        }

        .register-link {
            text-align: center;
            margin-top: 20px;
            color: #777;
        }

        .register-link a {
            color: var(--primary);
            text-decoration: none;
            font-weight: 600;
        }

        .register-link a:hover {
            text-decoration: underline;
        }

        .message {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
            display: none;
        }

        .message.error {
            background: #ffebee;
            color: #f44336;
            border: 1px solid #ffcdd2;
        }

        .message.success {
            background: #e8f5e9;
            color: #4caf50;
            border: 1px solid #c8e6c9;
        }

        @keyframes float {
            0% {
                transform: translateY(0);
            }
            50% {
                transform: translateY(-15px);
            }
            100% {
                transform: translateY(0);
            }
        }

        /* 响应式设计 */
        @media (max-width: 900px) {
            .login-container {
                flex-direction: column;
            }

            .login-image {
                padding: 30px;
            }

            .login-form {
                padding: 40px 30px;
            }
        }

        @media (max-width: 480px) {
            .header {
                flex-direction: column;
                gap: 15px;
                margin-bottom: 20px;
            }

            .social-login {
                flex-direction: column;
            }
        }

        /* 注册表单样式 */
        .register-form {
            display: none;
        }

        .form-toggle {
            text-align: center;
            margin-top: 20px;
            color: #777;
        }

        .form-toggle a {
            color: var(--primary);
            text-decoration: none;
            font-weight: 600;
            cursor: pointer;
        }

        .form-toggle a:hover {
            text-decoration: underline;
        }

        /* 性别选择器 */
        .gender-selector {
            display: flex;
            gap: 15px;
            margin-top: 5px;
        }

        .gender-option {
            flex: 1;
            padding: 12px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
        }

        .gender-option.selected {
            border-color: var(--primary);
            background-color: #f5f2ff;
        }

        .gender-option i {
            font-size: 20px;
            margin-bottom: 5px;
            display: block;
        }

        /* 生日选择器 */
        .birthday-selector {
            display: flex;
            gap: 10px;
        }

        .birthday-selector select {
            flex: 1;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-size: 16px;
            background: white;
        }

        /* 表单切换动画 */
        .form-section {
            transition: all 0.4s ease;
        }

        .user-details {
            padding: 15px;
            background: #f9f5ff;
            border-radius: 8px;
            margin: 10px;
        }

        .user-details div {
            display: flex;
            align-items: center;
            margin-bottom: 8px;
            font-size: 14px;
        }

        .user-details i {
            width: 20px;
            text-align: center;
            margin-right: 10px;
            color: var(--primary);
        }

        .user-email {
            font-size: 13px;
            color: #777;
            margin-top: 4px;
        }

    </style>
</head>
<body>
<div class="container">
    <!-- 顶部导航 -->
    <div class="header">
        <a href="index.html" class="logo">
            <i class="fas fa-gem"></i>
            POP MART
        </a>

        <div class="nav-icons">
            <a href="index.html" class="nav-icon">
                <i class="fas fa-home"></i>
            </a>
            <div class="nav-icon">
                <i class="fas fa-heart"></i>
            </div>
            <div class="nav-icon">
                <i class="fas fa-shopping-cart"></i>
                <span class="cart-count">3</span>
            </div>
        </div>
    </div>

    <!-- 登录区域 -->
    <div class="login-container">
        <div class="login-image">
            <div class="image-content">
                <div class="toy-icon">
                    <i class="fas fa-robot"></i>
                </div>
                <h2>欢迎来到泡泡玛特</h2>
                <p>探索最新潮玩系列，收集限量版盲盒，加入全球潮流玩具爱好者的大家庭！</p>
            </div>
        </div>

        <div class="login-form">
            <!-- 登录表单 -->
            <div id="loginFormSection" class="form-section">
                <h2>用户登录</h2>
                <p>请输入您的账号信息登录泡泡玛特</p>

                <div id="message" class="message"></div>

                <form id="loginForm">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <div class="input-with-icon">
                            <i class="fas fa-user"></i>
                            <input type="text" id="username" class="form-control" placeholder="请输入用户名" required
                                   maxlength="50">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="password">密码</label>
                        <div class="input-with-icon">
                            <i class="fas fa-lock"></i>
                            <input type="password" id="password" class="form-control" placeholder="请输入密码" required
                                   maxlength="50">
                        </div>
                    </div>

                    <div class="form-group">
                        <button type="submit" class="btn btn-accent">登 录</button>
                    </div>
                </form>

                <div class="divider">
                    <span>或使用以下方式登录</span>
                </div>

                <div class="social-login">
                    <div class="social-btn wechat">
                        <i class="fab fa-weixin"></i>
                        <span>微信</span>
                    </div>
                    <div class="social-btn qq">
                        <i class="fab fa-qq"></i>
                        <span>QQ</span>
                    </div>
                    <div class="social-btn weibo">
                        <i class="fab fa-weibo"></i>
                        <span>微博</span>
                    </div>
                </div>

                <div class="register-link">
                    还没有账号？<a id="showRegisterForm">立即注册</a>
                </div>
            </div>

            <!-- 注册表单 -->
            <div id="registerFormSection" class="form-section register-form">
                <h2>用户注册</h2>
                <p>创建账号，开启泡泡玛特收藏之旅</p>

                <div id="registerMessage" class="message"></div>

                <!-- 确保表单字段name与后端User实体一致 -->
                <form id="registerForm">
                    <div class="form-group">
                        <label for="uName">用户名</label>
                        <div class="input-with-icon">
                            <i class="fas fa-user"></i>
                            <input type="text" id="uName" name="uName" class="form-control" placeholder="请输入用户名"
                                   required maxlength="50">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="uPassword">密码</label>
                        <div class="input-with-icon">
                            <i class="fas fa-lock"></i>
                            <input type="password" id="uPassword" name="uPassword" class="form-control"
                                   placeholder="请输入密码" required maxlength="50">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="uEmail">邮箱</label>
                        <div class="input-with-icon">
                            <i class="fas fa-envelope"></i>
                            <input type="email" id="uEmail" name="uEmail" class="form-control" placeholder="请输入邮箱"
                                   required maxlength="50">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="uPhone">电话</label>
                        <div class="input-with-icon">
                            <i class="fas fa-phone"></i>
                            <input type="tel" id="uPhone" name="uPhone" class="form-control" placeholder="请输入电话"
                                   maxlength="20">
                        </div>
                    </div>

                    <div class="form-group">
                        <label>性别</label>
                        <div class="gender-selector">
                            <div class="gender-option" data-value="男">
                                <i class="fas fa-mars"></i>
                                <span>男</span>
                            </div>
                            <div class="gender-option" data-value="女">
                                <i class="fas fa-venus"></i>
                                <span>女</span>
                            </div>
                            <div class="gender-option" data-value="保密">
                                <i class="fas fa-genderless"></i>
                                <span>保密</span>
                            </div>
                        </div>
                        <input type="hidden" id="uSex" name="uSex" value="保密">
                    </div>

                    <div class="form-group">
                        <label for="uBirthday">生日</label>
                        <input type="date" id="uBirthday" name="uBirthday" class="form-control">
                    </div>

                    <div class="form-group">
                        <button type="submit" class="btn btn-accent">注 册</button>
                    </div>
                </form>

                <div class="form-toggle">
                    已有账号？<a id="showLoginForm">立即登录</a>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const loginForm = document.getElementById('loginForm');
        const registerForm = document.getElementById('registerForm');
        const messageDiv = document.getElementById('message');
        const registerMessageDiv = document.getElementById('registerMessage');
        const loginFormSection = document.getElementById('loginFormSection');
        const registerFormSection = document.getElementById('registerFormSection');
        const showRegisterForm = document.getElementById('showRegisterForm');
        const showLoginForm = document.getElementById('showLoginForm');
        const genderOptions = document.querySelectorAll('.gender-option');
        const uSexInput = document.getElementById('uSex');

        // 初始化生日字段为当前日期
        const today = new Date();
        const formattedDate = today.toISOString().split('T')[0];
        document.getElementById('uBirthday').value = formattedDate;

        // 性别选择器
        genderOptions.forEach(option => {
            option.addEventListener('click', function () {
                // 移除所有选项的选中状态
                genderOptions.forEach(opt => opt.classList.remove('selected'));

                // 设置当前选项为选中状态
                this.classList.add('selected');

                // 更新隐藏字段的值
                uSexInput.value = this.getAttribute('data-value');
            });
        });

        // 默认选择"保密"选项
        document.querySelector('.gender-option[data-value="保密"]').classList.add('selected');

        // 显示注册表单
        showRegisterForm.addEventListener('click', function (e) {
            e.preventDefault();
            loginFormSection.style.display = 'none';
            registerFormSection.style.display = 'block';
        });

        // 显示登录表单
        showLoginForm.addEventListener('click', function (e) {
            e.preventDefault();
            registerFormSection.style.display = 'none';
            loginFormSection.style.display = 'block';
        });

        // 修改登录表单提交逻辑
        loginForm.addEventListener('submit', function (e) {
            e.preventDefault();

            // 正确获取输入值
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            // 显示加载状态
            const submitBtn = loginForm.querySelector('button[type="submit"]');
            submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 登录中...';
            submitBtn.disabled = true;

            // 使用 URLSearchParams 构建请求体
            const params = new URLSearchParams();
            params.append('username', username);
            params.append('password', password);

            // 与后端API交互
            fetch('/user/login', {
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
            })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // 添加调试日志
                        console.log("登录成功，用户信息:", data.user);

                        // 确保保存完整的用户对象
                        localStorage.setItem('popmartUser', JSON.stringify(data.user));

                        // 显示成功消息
                        showMessage('登录成功！正在跳转...', 'success');

                        // 直接跳转到首页
                        setTimeout(() => {
                            window.location.href = 'index.html';
                        }, 1500);
                    } else {
                        showMessage(data.message || '登录失败，请检查用户名和密码', 'error');
                        submitBtn.innerHTML = '登 录';
                        submitBtn.disabled = false;
                    }
                })
                .catch(error => {
                    showMessage('网络错误，请稍后再试: ' + error.message, 'error');
                    submitBtn.innerHTML = '登 录';
                    submitBtn.disabled = false;
                });
        });

        // 注册表单提交
        registerForm.addEventListener('submit', function (e) {
            e.preventDefault();

            // 收集表单数据
            const user = {
                uName: document.getElementById('uName').value,
                uPassword: document.getElementById('uPassword').value,
                uEmail: document.getElementById('uEmail').value,
                uPhone: document.getElementById('uPhone').value,
                uSex: uSexInput.value,
                uBirthday: document.getElementById('uBirthday').value
            };

            // 显示加载状态
            const submitBtn = registerForm.querySelector('button[type="submit"]');
            submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 注册中...';
            submitBtn.disabled = true;

            // 与后端API交互
            fetch('/user/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(user)
            })
                .then(response => {
                    if (!response.ok) {
                        return response.json().then(err => {
                            throw new Error(err.message || '注册失败')
                        });
                    }
                    return response.json();
                })
                .then(data => {
                    if (data.success) {
                        showRegisterMessage('注册成功！请登录', 'success');

                        // 自动填充登录表单
                        document.getElementById('username').value = user.uName;

                        // 清空表单
                        registerForm.reset();

                        // 重置性别选择
                        genderOptions.forEach(opt => opt.classList.remove('selected'));
                        document.querySelector('.gender-option[data-value="保密"]').classList.add('selected');
                        uSexInput.value = '保密';

                        // 重置生日
                        document.getElementById('uBirthday').value = formattedDate;

                        setTimeout(() => {
                            registerFormSection.style.display = 'none';
                            loginFormSection.style.display = 'block';
                        }, 1500);
                    } else {
                        showRegisterMessage(data.message || '注册失败，请稍后再试', 'error');
                    }
                    submitBtn.innerHTML = '注 册';
                    submitBtn.disabled = false;
                })
                .catch(error => {
                    showRegisterMessage(error.message || '网络错误，请稍后再试', 'error');
                    submitBtn.innerHTML = '注 册';
                    submitBtn.disabled = false;
                });
        });

        // 显示登录消息
        function showMessage(text, type) {
            messageDiv.textContent = text;
            messageDiv.className = 'message ' + type;
            messageDiv.style.display = 'block';

            // 3秒后自动消失
            setTimeout(() => {
                messageDiv.style.display = 'none';
            }, 3000);
        }

        // 显示注册消息
        function showRegisterMessage(text, type) {
            registerMessageDiv.textContent = text;
            registerMessageDiv.className = 'message ' + type;
            registerMessageDiv.style.display = 'block';

            // 3秒后自动消失
            setTimeout(() => {
                registerMessageDiv.style.display = 'none';
            }, 3000);
        }
    });

    // 更新用户状态显示
    function updateUserStatus(user) {
        loginText.textContent = user.uName;
        userName.textContent = user.uName;
        userAvatar.textContent = user.uName.charAt(0).toUpperCase();
        userAvatar.style.background = 'linear-gradient(135deg, #ff6ec7, #00c8ff)';
        isLoggedIn = true;

        // 显示用户详细信息
        document.getElementById('userPhone').textContent = user.uPhone || '未设置';
        document.getElementById('userGender').textContent = user.uSex || '未设置';

        // 格式化生日
        if (user.uBirthday) {
            const birthday = new Date(user.uBirthday);
            document.getElementById('userBirthday').textContent =
                `${birthday.getFullYear()}-${(birthday.getMonth() + 1).toString().padStart(2, '0')}-${birthday.getDate().toString().padStart(2, '0')}`;
        } else {
            document.getElementById('userBirthday').textContent = '未设置';
        }
    }

    // 用户图标点击事件
    userIcon.addEventListener('click', function (e) {
        if (!isLoggedIn) {
            window.location.href = 'login.html';
        } else {
            const savedUser = JSON.parse(localStorage.getItem('popmartUser'));
            console.log("本地存储的用户信息:", savedUser); // 添加这行

            if (savedUser) {
                updateUserStatus(savedUser);
            }
            userStatus.classList.toggle('active');
        }
    });

    // 页面加载时检查登录状态
    document.addEventListener('DOMContentLoaded', function () {
        // 检查本地存储中的登录状态
        const savedUser = localStorage.getItem('popmartUser');
        let isLoggedIn = false;

        if (savedUser) {
            const user = JSON.parse(savedUser);
            updateUserStatus(user);
            isLoggedIn = true;
        }

        // ... 其他初始化代码 ...
    });

    if (user.uBirthday) {
        document.getElementById('userBirthday').textContent = user.uBirthday;
    } else {
        document.getElementById('userBirthday').textContent = '未设置';
    }

</script>
</body>
</html>